package com.yumingjiang.expand.aware;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.function.Consumer;

/**
 * @description: Aware做属性注入的逻辑验证
 * <p>
 *  1、实现Aware接口，对象在实例化完成后，对在填充完属性后，调用其实现的方法做赋值操作，
 *  所以我这边在初始化的时候会预先创建一个Env启动的时候Debug看效果是否这样。
 *  2、实现Init接口，因为容器启动完成后、在经历了属性填充、Aware方法回调后，在执行BeanPostProcessor的两个方法执行间隙会执行该方法
 * @author: John
 * @version: EnvInfoShow.java, v1.0 2025年01月05日 02:49
 */
@Slf4j
@Component
public class EnvInfoShow implements EnvironmentAware, InitializingBean {
    private Environment environment = new StandardEnvironment();

    public Environment getEnvironment() {
        //输出Profile的名称
        Arrays.stream(this.environment.getActiveProfiles()).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                log.info(">>>>>>>>>> ProfileName: "+s+" <<<<<<<<<");
            }
        });

        log.info("project.name:"+environment.getProperty("spring.application.name"));
        return environment;
    }

    @Override
    public void setEnvironment(Environment environment) {

        this.environment=environment;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        getEnvironment();
        log.info("Environment interface was invoked. at:"+new Date());
    }
}